package com.sskj.gcc.ui.fragment;


import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.gcc.R;
import com.sskj.gcc.bean.game.GameCommunitiesListBean;
import com.sskj.gcc.presenter.game.GameFragmentPresenter;
import com.sskj.gcc.ui.dialog.GameBottomDialog;
import com.sskj.gocoin.common.adapter.BaseAdapter;
import com.sskj.gocoin.common.adapter.BaseRecyclerAdapter;
import com.sskj.gocoin.common.adapter.RecyViewHolder;
import com.sskj.gocoin.common.adapter.ViewHolder;
import com.sskj.gocoin.m.HttpConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.bean.CoinBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;


public class GameBottomMallFragment extends GameBaseFragment {


    @BindView(R.id.rv_list)
    RecyclerView recyclerView;

    private BaseRecyclerAdapter<GameCommunitiesListBean> mAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.app_fragment_game_mall;
    }

    @Override
    public GameFragmentPresenter getPresenter() {
        return new GameFragmentPresenter();
    }

    @Override
    public void initView() {

        GradientDrawable background0 = new GradientDrawable();
        background0.setCornerRadius(10);
        background0.setColor(Color.parseColor("#E68A47"));

        GradientDrawable background1 = new GradientDrawable();
        background1.setCornerRadius(10);
        background1.setColor(Color.parseColor("#9A39DD"));

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new BaseRecyclerAdapter<GameCommunitiesListBean>(getActivity(), R.layout.app_adapter_game_mall) {
            @Override
            public void convert(RecyViewHolder holder, GameCommunitiesListBean data, int position) {
                if (position % 2 == 0) {
                    Glide.with(getActivity()).load(background0).into((ImageView) holder.getView(R.id.iv_background));
                    holder.setTextColor(R.id.tv_buy, Color.parseColor("#E68A47"));
                } else {
                    Glide.with(getActivity()).load(background1).into((ImageView) holder.getView(R.id.iv_background));
                    holder.setTextColor(R.id.tv_buy, Color.parseColor("#9A39DD"));
                }

                if ("1".equals(data.getType())) {
                    holder.setText(R.id.tv_buy, getString(R.string.strMainActivityGame00022));
                } else if ("2".equals(data.getType())) {
                    holder.setText(R.id.tv_buy, getString(R.string.strMainActivityGame00023));
                } else if ("3".equals(data.getType())) {
                    holder.setText(R.id.tv_buy, getString(R.string.strMainActivityGame00024));
                }

                Glide.with(getActivity()).load(HttpConfig.BASE_URL + "/" + data.getImg()).into((ImageView) holder.getView(R.id.iv_logo));

                holder.setText(R.id.tv_name, data.getName());
                holder.setText(R.id.tv_count, "X" + data.getNums());
                holder.setText(R.id.tv_content, data.getMsg());
                holder.setText(R.id.tv_price, data.getPrice() + " USDT");

                // 类型 1 允许操作 2 未开启 禁止点击
                if ("1".equals(data.getStatus())) {
                    holder.getView(R.id.tv_buy).setClickable(true);
                    ClickUtil.click(holder.getView(R.id.tv_buy), new ClickUtil.Click() {
                        @Override
                        public void click() {
                            RxBus.getDefault().send(RxBusCode.GAME_SHOP_CONFIRM, data);
                            getActivity().finish();
                        }
                    });
                } else if ("2".equals(data.getStatus())) {
                    holder.getView(R.id.tv_buy).setClickable(false);
                }

            }
        };
        recyclerView.setAdapter(mAdapter);
        mPresenter.getCommunitiesList();
    }

    @Override
    public void getCommunitiesListSuccess(List<GameCommunitiesListBean> listBeans) {
        super.getCommunitiesListSuccess(listBeans);
        if (listBeans != null) {
            mAdapter.setDatas(listBeans);
        }
    }

    /**
     * @param data
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshCoin(CoinBean data) {

    }

}
